package net.tngou.tpage.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.math.NumberUtils;

import net.tngou.tpage.dao.TemplateDao;
import net.tngou.tpage.jdbc.PageUtil;
import net.tngou.tpage.pojo.POJO;
import net.tngou.tpage.pojo.Tag;
import net.tngou.tpage.pojo.Tag_template;
import net.tngou.tpage.pojo.Tempdata;
import net.tngou.tpage.pojo.Template;
import net.tngou.tpage.util.Pinyin4j;
import net.tngou.tpage.util.StringUtil;

/**
 * 模板
 * @author tngou@tngou.net
 *
 */
public class TemplateService extends BaseService{
	/**
	 * 取得模板
	 * @param page
	 * @param size
	 * @param user
	 * @param tags
	 * @param keyword
	 * @return
	 */
	public PageUtil getPage(int page,int size,long user,String [] tags,String keyword) {
		int[] tag ={};
		for (String i : tags) {
			if(NumberUtils.isNumber(i))
				tag=ArrayUtils.add(tag, Integer.parseInt(i));
		}
		TemplateDao dao = new TemplateDao();		
		return new PageUtil(dao.list(page, size, user, tag, keyword), page, size, dao.total(user, tag, keyword));		
	}
	
	/**
	 * 取得标签
	 * @param user
	 * @return
	 */
	public List<Map<String, Object>> tags(long user) {		
		TemplateDao dao = new TemplateDao();		
		return dao.tags(user);
	}
	
	/**
	 * 保存模板
	 * @param template
	 * @return
	 */
	@SuppressWarnings("serial")
	public long save(Template template) {
		String tags= _replace(template.getTags());
		String[] ts = StringUtil.split(tags, ",");
		String t="";
		for (String string : ts) {
			t=t+string+",";
		}
		t=t.substring(0,t.length()-1);
		template.setTags(t);
		long id = template.save();
		if(id>0)
		{
			for (String string : ts) {
				Tag tag = new Tag().get(new HashMap<String,Object>(){{put("name",string);put("user",template.getUser());}});
				if(tag!=null)
				{
					Tag_template tag_template = new Tag_template();
					tag_template.setTag(tag.getId());tag_template.setTemplate(id);
					tag_template.save();
				}else
				{
					tag = new Tag();
					tag.setName(string);
					tag.setPinyin(Pinyin4j.ToHanyuPinyinHead(string, true));//设置拼音
					tag.setUser(template.getUser());
					long tagid = tag.save();
					Tag_template tag_template = new Tag_template();
					tag_template.setTag(tagid);tag_template.setTemplate(id);
					tag_template.save();
				}
			}
			
		}		
		return id;
	}
	
	/**
	 * 更新
	 * @param bean
	 * @param template
	 * @return 
	 */
	@SuppressWarnings("serial")
	public long update(Template bean,Template template) {
		
		if(StringUtil.isNotEmpty(bean.getName()))template.setName(bean.getName());
		if(StringUtil.isNotEmpty(bean.getDescription()))template.setDescription(bean.getDescription());
		if(StringUtil.isNotEmpty(bean.getPinyin()))template.setPinyin(bean.getPinyin().toUpperCase());
		if(StringUtil.isNotEmpty(bean.getTags())&&!bean.getTags().equals(template.getTags())){
			String tags= _replace(bean.getTags());
			String[] ts = StringUtil.split(tags, ",");
			String t="";
			for (String string : ts) {
				t=t+string+",";
			}
			t=t.substring(0,t.length()-1);
			template.setTags(t);
			new Tag_template().delete("template="+template.getId());//删除原始tag
			for (String string : ts) {
				Tag tag = new Tag().get(new HashMap<String,Object>(){{put("name",string);put("user",template.getUser());}});
				if(tag!=null)
				{
					Tag_template tag_template = new Tag_template();
					tag_template.setTag(tag.getId());tag_template.setTemplate(template.getId());
					tag_template.save();
				}else
				{
					tag = new Tag();
					tag.setName(string);
					tag.setPinyin(Pinyin4j.ToHanyuPinyinHead(string, true));//设置拼音
					tag.setUser(template.getUser());
					long tagid = tag.save();
					Tag_template tag_template = new Tag_template();
					tag_template.setTag(tagid);tag_template.setTemplate(template.getId());
					tag_template.save();
				}
			}
		}
		return template.update();
		
	}
	
	/**
	 * 拷贝数据
	 * @param template
	 * @return
	 */
	@SuppressWarnings({ "serial", "unchecked" })
	public long copy(Template template,long user) {
		Template bean = template;
		bean.setId(0);
		bean.setUser(user);
		long id = bean.save();
	    Map<String, Object> map = new HashMap<String,Object>(){{put("template", template.getId());}};
	    List<Tempdata> list = (List<Tempdata>) new Tempdata().list(map);//保存数据
	    list.forEach(e->{
	    	e.setId(0);
	    	e.setTemplate(id);
	    	e.setUser(user);
	    	e.save();
	    });
	    
		List<Tag> ls = (List<Tag>) new Tag().list(map); 
		ls.forEach(e->{
			if(e.getUser()==user)
			{
				 Tag_template tag_t = new Tag_template();
				 tag_t.setTag(e.getId());
				 tag_t.setTemplate(id);
				 tag_t.save();
				
			}else
			{
				
				Tag tag = new Tag().get(new HashMap<String,Object>(){{put("name",e.getName());put("user",user);}});
				if(tag!=null)
				{
					Tag_template tag_template = new Tag_template();
					tag_template.setTag(tag.getId());tag_template.setTemplate(id);
					tag_template.save();
				}else
				{
					tag = new Tag();
					tag.setName(e.getName());
					tag.setPinyin(e.getPinyin());//设置拼音
					tag.setUser(user);
					long tagid = tag.save();
					Tag_template tag_template = new Tag_template();
					tag_template.setTag(tagid);tag_template.setTemplate(id);
					tag_template.save();
				}
			}
			
		});
	
		return id;
	}
	//替换  处理标签
	private String _replace(String tags) {
		tags=StringUtil.replace(tags, " ", ",");
		tags=StringUtil.replace(tags, ";", ",");
		tags=StringUtil.replace(tags, "，", ",");
		tags=StringUtil.replace(tags, "；", ",");
		return tags;
	}

	
}
